package com.etc.Filter;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.etc.controller.Counter;

public class MyInterceptor implements HandlerInterceptor {

	@Override
	public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
			throws Exception {
		// TODO Auto-generated method stub

	}

	@Override
	public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)
			throws Exception {
		// TODO Auto-generated method stub

	}

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handle) throws Exception {
		judgeIp(request, response);
		/*if (request.getSession().getAttribute("user") == null) {
			response.sendRedirect("login.jsp");
			return false;
		}*/
		return true;
	}

	public void wirtercount(HttpServletRequest request, HttpServletResponse response) {
		Counter CountFileHandler = new Counter();
		int count = 0;
		if (request.getServletContext().getAttribute("count") == null) {
			count = CountFileHandler.readFile(request.getRealPath("/") + "count.txt");
			request.getServletContext().setAttribute("count", new Integer(count));
		}
		count = (Integer) request.getServletContext().getAttribute("count");
		++count;
		request.getServletContext().setAttribute("count", count);
		CountFileHandler.writeFile(request.getRealPath("/") + "count.txt", count);
		//System.out.println("count:"+count);
	}

	public void judgeIp(HttpServletRequest request, HttpServletResponse response) {
		String ip = request.getHeader("x-forwarded-for");

		if (null == ip || 0 == ip.length() || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("Proxy-Client-IP");
		}
		if (null == ip || 0 == ip.length() || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("WL-Proxy-Client-IP");
		}
		if (null == ip || 0 == ip.length() || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("X-Real-IP");
		}
		if (null == ip || 0 == ip.length() || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getRemoteAddr();
		}
		if (request.getServletContext().getAttribute("ipMap") == null) {
			Map<String, Long> ipMap = new HashMap<String, Long>();
			ipMap.put(ip, System.currentTimeMillis());
			request.getServletContext().setAttribute("ipMap", ipMap);
			wirtercount(request, response);
		} else {
			Map<String, Long> ipMap = (Map<String, Long>) request.getServletContext().getAttribute("ipMap");
			if (ipMap.containsKey(ip)) {
				Long before = ipMap.get(ip);
				Long now = System.currentTimeMillis();
				Long dvalue = now - before;
				if (dvalue > (1000 * 60 * 60)) {
					wirtercount(request, response);
				}
			}else {
				ipMap.put(ip, System.currentTimeMillis());
				wirtercount(request, response);
			}
			
		}
		
		//System.out.println("ip:"+ip);
	}
}
